# 标准库
import os


# print(os.getcwd())

# os.chdir("..")

# print(os.getcwd())

# os.system('echo "hello world!"')
# os.system('dir')

# # dir <返回由模块的所有函数组成的列表>
# print(dir(os))

# # help <返回根据模块文档字符串创建的详细说明页面>
# print(help(os))

# 日常文件和目录管理任务， shutil 模块提供了更易于使用的更高级别的接口:
# import shutil

# 终止脚本的最直接方法是使用 sys.exit() 

# import random
# # 随机 (0-100)
# print(random.randrange(100))

# # 简单统计
# import statistics
# data = [2.75, 1.75, 1.25, 0.25, 0.5, 1.25, 3.5]

# # 平均
# print(statistics.mean(data))

# # 中值
# print(statistics.median(data))

# # 方差
# print(statistics.variance(data))

# # 基本网络访问
# import urllib
# import urllib.request

# with urllib.request.urlopen("https://gitee.com/top_xiong") as response:
#     info = ""
#     for line in response:
#         line = line.decode()
#         info += line + "\n"
#         print(line)

# # 发送邮件
# import smtplib
# from email.mime.text import MIMEText
# from email.utils import formataddr

# # 配置邮箱参数
# mail_host = "smtp.qq.com"  # QQ邮箱服务器地址
# mail_port = 587  # 使用 TLS 端口
# sender_name = "小熊"
# sender = "1263083401@qq.com"  # 发件人邮箱
# password = "lozxaplbufplbacg"  # 邮箱授权码（不是QQ密码！）
# receivers = "xingranna@gmail.com"  # 收件人邮箱列表

# # 1. 构建符合RFC标准的邮件
# msg = MIMEText(info, "html", "utf-8")
# msg["From"] = formataddr((sender_name, sender))  # 关键修复点！
# msg["To"] = receivers
# msg["Subject"] = "测试邮件主题"

# # 2. 发送邮件
# try:
#     # 创建TLS连接
#     with smtplib.SMTP(mail_host, mail_port) as server:
#         server.starttls()  # 关键步骤：启用TLS加密
#         # server.set_debuglevel(1) 
#         server.login(sender, password)
#         server.sendmail(sender, receivers, msg.as_string())
#         print("服务器响应:", server.noop()[1])  # 检查连接状态
#         if "250" in server.noop()[1].decode():
#             print("邮件确认已送达服务器")
# except Exception as e:
#     print(f"邮件发送失败: {e}")\


# # 日期日期
# from datetime import date
# today = date.today()
# print(today)

# print(today.strftime("%m-%d-%y. %d %b %Y is a %A on the %d day of %B."))

# # 数据压缩  包括：zlib, gzip, bz2, lzma, zipfile 和 tarfile。:
# import zlib
# s = b'witch which has which witches wrist watch'

# print(len(s))

# t = zlib.compress(s)
# print(len(t))
# t2 = zlib.compress(t)
# print(len(t2))

# print(zlib.decompress(zlib.decompress(t2)))

# """
# 41
# 37
# 46      # 压缩次数无效的 哈哈哈
# b'witch which has which witches wrist watch'
# """

# # 性能测试

# # time
# import time

# start = time.perf_counter()
# result = sum(range(10**5))
# end = time.perf_counter()

# print(f"耗时:{end-start:.6f}秒")

# # 关键代码耗时
# import timeit
# #直接测量函数用时,并重复多次
# t = timeit.timeit(lambda:sum(range(10**6)), number=100)

# print(f"平均耗时: {t/100:.4f} 秒")

# 重点:cProfile

# def example():
#     return sum(i*j for i in range(1000) for j in range(1000))

# def example2():
#     return sum(i**j for i in range(1000) for j in range(1000))

# def test():
#     example()
#     example2()

# print("start")
# print(test())
# print("end")


# import cProfile
# cProfile.run("test()", sort="cumtime") # 按累计时间排序

# 可视化
# python -m cProfile -s cumtime -o profile.out test1.py
# snakeviz profile.out 
# py-spy record -o profile.svg -- python main.py

# 优化案例

# # 递归计算斐波那契数列（性能差）
# def fib(n):
#     return n if n < 2 else fib(n-1) + fib(n-2)

# # 优化方案：迭代或缓存
# from functools import lru_cache
# @lru_cache(maxsize=None)
# def fib_opt(n):
#     return n if n < 2 else fib_opt(n-1) + fib_opt(n-2)

# def test():
#     print(fib(30))
#     print(fib(30))
#     print(fib(30))
#     print(fib(30))
#     print(fib_opt(30))
#     print(fib_opt(30))
#     print(fib_opt(30))
#     print(fib_opt(30))


# test()

# 
import struct

data = b'\x01\x02\x03\x04'  # 4字节数组
format = '4B'               # 4个unsigned char

result = struct.unpack(format, data)
print(result)  # 输出: (1, 2, 3, 4)